#include "chooselevelscene.h"
#include<QMenuBar>
#include<QPainter>
#include"mypushbutton.h"
#include<QDebug>
#include<QTimer>
#include<QLabel>
#include<QSound>

ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //配置选择关卡场景
    this->setFixedSize(320,588);

    //设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));

    //设置标题
    this->setWindowTitle("选择关卡场景");

    //创建菜单栏
    QMenuBar * bar = menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu * starMenu = bar->addMenu("开始");

    //创建 退出菜单项
    QAction * quitAction = starMenu->addAction("退出");

    //点击退出 退出游戏
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //选择关卡音效
    QSound * chooseSound = new QSound(":/res/TapButtonSound.wav");
    //返回按钮音效
    QSound * backSound = new QSound(":/res/TapButtonSound.wav");

    //返回按钮
    MyPushBUtton * backBtn = new MyPushBUtton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width() - backBtn->width(),this->height()-backBtn->height());

    //点击返回
    connect(backBtn,&MyPushBUtton::clicked,[=](){
        qDebug() << "点击了返回按钮";
        //播放返回按钮音效
        backSound->play();
        //告诉主场景 返回  主场景监听 chooseLevelScene的返回按钮
        QTimer::singleShot(500,this,[=](){
            emit this->chooseSceneBack();   //发送自定义信号
        });

    });

    //创建关卡按钮
    for(int i = 0; i <20;i++)
    {
        MyPushBUtton * menuBtn = new MyPushBUtton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25 + i % 4 * 70,130 + i / 4 * 70);

        //监听每个按钮的点击事件
        connect(menuBtn,&MyPushBUtton::clicked,[=](){
            //播放选择关卡音效
            chooseSound->play();
            QString str = QString("选择的第 %1 关").arg(i+1);
            qDebug() << str;

            //进入到游戏场景
            this->hide();
            play = new PlayScene(i+1);

            //设置游戏场景的初始位置
            play->setGeometry(this->geometry());

            play->show();

            connect(play,&PlayScene::chooseSceneBack,[=](){
                this->setGeometry(play->geometry());
                this->show();
                delete play;
                play = NULL;

            });

        });

        QLabel * label = new QLabel;
        label->setParent(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        label->move(25 + i % 4 * 70,130 + i / 4 * 70);
        //设置对齐方式
        label->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
        //设置让鼠标进行穿透  51号
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }

}

void ChooseLevelScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    pix.load(":/res/Title.png");
    painter.drawPixmap(this->width()*0.5-pix.width()*0.5,30,pix.width(),pix.height(),pix);
}
